package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.AlarmRuleService;
import com.tutu.service.LogInfoService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 告警规则管理
 */
@Controller
@RequestMapping(value = "backend/alarmRule")
public class AlarmRuleController extends CommonController {

    @Autowired
    AlarmRuleService alarmRuleService;

    @Autowired
    LogInfoService logInfoService;

    @RequestMapping("/list")
    public void list(HttpServletResponse response, String deviceCode, Integer type, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, alarmRuleService.pageByParams(deviceCode, type, null, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request,
                                    Integer id,
                                    Integer dataType,
                                    String deviceCode,
                                    Integer type,
                                    String codeDictName,
                                    String codeDictUnit,
                                    Double blueNum,
                                    Double yellowNum,
                                    Double redNum,
                                    Integer alarmType,
                                    Integer alarmTime) {
        return alarmRuleService.saveInfo(request, id, dataType, deviceCode, type, codeDictName, codeDictUnit, blueNum, yellowNum, redNum, alarmType, alarmTime);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return alarmRuleService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return alarmRuleService.batchDel(request, ids);
    }

    @RequestMapping("/batchAdd")
    @ResponseBody
    public Map<String, Object> batchAdd(HttpServletRequest request,
                                        String deviceCode,
                                        Integer type,
                                        String codeStr,
                                        String sizeStr,
                                        String blueNumStr,
                                        String yellowNumStr,
                                        String redNumStr,
                                        String typeStr,
                                        String timeStr) {
        return alarmRuleService.batchAdd(request, deviceCode, type, codeStr, sizeStr, blueNumStr, yellowNumStr, redNumStr, typeStr, timeStr);
    }

    @RequestMapping("/sync")
    @ResponseBody
    public Map<String, Object> sync(HttpServletRequest request) {
        return alarmRuleService.syncData(request);
    }

    @RequestMapping("/cleanRepeatData")
    @ResponseBody
    public Map<String, Object> cleanRepeatData(HttpServletRequest request) {
        return alarmRuleService.cleanRepeatData(request);
    }
}
